package srzy.module

import java.io.FileWriter
import java.io.StringWriter
import srzy.dao.DBUtil

object DBRec extends AbsCaseDeal with srzy.common.Logging {

  val caseInfoSource = DBUtil("CaseInfoSource")

  def boolToStr(b: Boolean) = if (b == true) "pass" else "fail"

  def deal = {
    logger.debug("dbCompInfos record")
    getCaseInfo.dbCompInfos.foreach(ci => {
      val sqlList = List("DELETE FROM OCS_AUTO_CASE_CHECK_DATA WHERE CASE_ID = '"
        + getCaseInfo.caseName + "' AND ORDER_ID = " + ci.oraderId,
        "INSERT INTO OCS_AUTO_CASE_CHECK_DATA (CASE_ID,ORDER_ID,CASE_TYPE,CHECK_PARA,PRE_RESULT,DATA_RESULT,PASS_FLAG) VALUES ('"
          + getCaseInfo.caseName
          + "', '"
          + ci.oraderId
          + "', 'DB' , '"
          + ci.assembledSQL.replace("'", "''")
          + "', '"
          + ci.preResult.replace("'", "''")
          + "', '"
          + ci.result.replace("'", "''")
          + "', '" + boolToStr(ci.compResult) + "')")
      caseInfoSource.executeUpdate(sqlList)
    })

    logger.debug("shellCompInfos record")
    getCaseInfo.shellCompInfos.foreach(sci => {
      val sqlList = List("DELETE FROM OCS_AUTO_CASE_CHECK_DATA WHERE CASE_ID = '"
        + getCaseInfo.caseName + "' AND ORDER_ID = " + sci.oraderId,
        "INSERT INTO OCS_AUTO_CASE_CHECK_DATA (CASE_ID,ORDER_ID,CASE_TYPE,CHECK_PARA,PRE_RESULT,DATA_RESULT,PASS_FLAG) VALUES ('"
          + getCaseInfo.caseName
          + "', '"
          + sci.oraderId
          + "', 'SHELL' , '"
          + sci.shell.replace("'", "''")
          + "', '"
          + sci.preResult.replace("'", "''")
          + "', '"
          + sci.result.replace("'", "''")
          + "', '" + boolToStr(sci.compResult) + "')")
      caseInfoSource.executeUpdate(sqlList)
    })

    logger.debug("dbCompInfos total record")

    if (getCaseInfo.dbCompInfos.size != 0) {
      val sqlList = List("DELETE FROM OCS_AUTO_CASE_CHECK_RESULT WHERE CASE_ID = '"
        + getCaseInfo.caseName + "'",
        "INSERT INTO OCS_AUTO_CASE_CHECK_RESULT(CASE_ID, CASE_TYPE, PASS_FLAG) VALUES('"
          + getCaseInfo.caseName
          + "', 'DB','"
          + boolToStr(getCaseInfo.dbCompResult) + "')")
      caseInfoSource.executeUpdate(sqlList)
    }

    logger.debug("shellCompInfos total record")

    if (getCaseInfo.shellCompInfos.size != 0) {
      val sqlList = List("DELETE FROM OCS_AUTO_CASE_CHECK_RESULT WHERE CASE_ID = '"
        + getCaseInfo.caseName + "'",
        "INSERT INTO OCS_AUTO_CASE_CHECK_RESULT(CASE_ID, CASE_TYPE, PASS_FLAG) VALUES('"
          + getCaseInfo.caseName
          + "', 'SHELL','"
          + boolToStr(getCaseInfo.shellCompResult) + "')")
      caseInfoSource.executeUpdate(sqlList)
    }

    val c = caseInfoSource.executeLine("SELECT COUNT(*) FROM OCS_AUTO_CASE_ONLINE WHERE CASE_PWD = '" + getCaseInfo.casePath + "'")
    if (c == "0") {
      caseInfoSource.executeUpdate("INSERT INTO OCS_AUTO_CASE_ONLINE(CASE_PWD, DEAL_FLAG, DEAL_DATE) VALUES ('" + getCaseInfo.casePath + "', '0', SYSDATE)")
    }
    val flag = if (getCaseInfo.caseResult == false) 2 else 1
    caseInfoSource.executeUpdate("UPDATE OCS_AUTO_CASE_ONLINE SET DEAL_FLAG = " + flag + " ,DEAL_DATE = SYSDATE WHERE CASE_PWD = '" + getCaseInfo.casePath + "'")

  }
}